/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.process;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 *
 * Run tasks in background
 *
 * @author risto
 */
public class Exec implements Runnable {
    static Queue<BGTask> queue = new ArrayDeque<BGTask>();
    static Exec ref;
    static Thread execThread;

    public static void queue(BGTask t) {
        queue.add(t);
    }

    static {
        ref = new Exec();
        execThread = new Thread(ref);
        execThread.start();
    }

    @Override
    public void run() {
        while(true) {
            if (queue.isEmpty()) {
                try { Thread.sleep(500); } catch (InterruptedException ie) {}
                continue;
            }

            BGTask t = queue.poll();
            t.doTask();

            // post sleep
            try { Thread.sleep(100); } catch (InterruptedException ie) {}
        }
    }


}
